package groupOne.app.DWS;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.PropertyNamingStrategy;
import com.alibaba.fastjson.serializer.SerializeConfig;
import groupOne.app.BaseAppOneStream;
import groupOne.bean.TestExamQuestionBean;
import groupOne.common.Constant;
import groupOne.util.AtguiguUtil;
import groupOne.util.DimAsyncFunction;
import groupOne.util.FlinkSinkUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.AsyncDataStream;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

/**
 *drop table if exists dws_test_user_test_exam_question_window;
 * create table if not exists dws_test_user_test_exam_question_window(
 *       stt datetime,
 *       edt datetime,
 *       cur_date date,
 *       question_id varchar(20),
 *       question_txt varchar(2000),
 *
 *       answer_correct_ct bigint replace comment '答对次数',
 *       answer_ct bigint replace comment '答题次数',
 *       u_answer_correct_ct bigint replace comment '答对题独立用户数',
 *       u_answer_ct bigint replace comment '答题独立用户数'
 * )
 * engine = olap
 * aggregate key(stt,edt,cur_date,question_id,question_txt)
 * comment "考试域答题汇总表"
 * partition by range(cur_date)()
 * distributed by hash(stt) buckets 10 properties(
 *     "replication_num" = "3",
 *   "dynamic_partition.enable" = "true",
 *   "dynamic_partition.time_unit" = "DAY",
 *   "dynamic_partition.start" = "-1",
 *   "dynamic_partition.end" = "1",
 *   "dynamic_partition.prefix" = "par",
 *   "dynamic_partition.buckets" = "10",
 *   "dynamic_partition.hot_partition_num" = "3"
 * );
 */
public class Dws_03_TestUserTestExamQuestionXiang extends BaseAppOneStream {
    public static void main(String[] args) {
        new Dws_03_TestUserTestExamQuestionXiang().init(
                6008,
                2,
                "Dws_03_TestUserTestExamQuestionXiang",
                Constant.TOPIC_DWD_TEST_TEST_EXAM_QUESTION
        );
    }
    @Override
    protected void handle(StreamExecutionEnvironment env, DataStreamSource<String> stream) {
        //封装bean
        DataStream<TestExamQuestionBean> pojoStream = transFunc(stream);

        //reduce
        SingleOutputStreamOperator<TestExamQuestionBean> aggStream = aggFunc(pojoStream);

        //dim
        SingleOutputStreamOperator<TestExamQuestionBean> addDimStream = addDim(aggStream);
        //写入doris
//        addDimStream.print();
        writeDoris(addDimStream);
    }

    private SingleOutputStreamOperator<TestExamQuestionBean> addDim(SingleOutputStreamOperator<TestExamQuestionBean> aggStream) {
        return AsyncDataStream.unorderedWait(
                aggStream,
                new DimAsyncFunction<TestExamQuestionBean>() {
                    @Override
                    public String getTable() {
                        return "dim_test_question_info";
                    }

                    @Override
                    public String getId(TestExamQuestionBean input) {
                        return input.getQuestionId();
                    }

                    @Override
                    public void addDim(JSONObject dim, TestExamQuestionBean bean) {
                        bean.setQuestionTxt(dim.getString("QUESTION_TXT"));
                    }
                },
                60,
                TimeUnit.SECONDS
        );
    }

    private void writeDoris(SingleOutputStreamOperator<TestExamQuestionBean> aggStream) {
        aggStream
                .map(bean -> {
                    SerializeConfig config = new SerializeConfig();
                    config.propertyNamingStrategy= PropertyNamingStrategy.SnakeCase;
                    return JSON.toJSONString(bean,config);
                })
                .addSink(FlinkSinkUtil.getDorisSink("edu.dws_test_user_test_exam_question_window"));
    }

    private SingleOutputStreamOperator<TestExamQuestionBean> aggFunc(DataStream<TestExamQuestionBean> pojoStream) {
        return pojoStream
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<TestExamQuestionBean>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                                .withTimestampAssigner((t,l)->t.getTs())
                )
                .keyBy(TestExamQuestionBean::getQuestionId)
                .window(TumblingEventTimeWindows.of(Time.seconds(5)))
                .reduce(new ReduceFunction<TestExamQuestionBean>() {
                            @Override
                            public TestExamQuestionBean reduce(TestExamQuestionBean bean1, TestExamQuestionBean bean2) throws Exception {
                                bean1.setAnswerCorrectCt(bean1.getUAnswerCorrectCt() + bean2.getAnswerCorrectCt());
                                bean1.setAnswerCt(bean1.getAnswerCt() + bean2.getAnswerCt());
                                bean1.setUAnswerCorrectCt(bean1.getUAnswerCorrectCt() + bean2.getUAnswerCorrectCt());
                                bean1.setUAnswerCt(bean1.getUAnswerCt() + bean2.getUAnswerCt());
                                return bean1;
                            }
                        },
                        new ProcessWindowFunction<TestExamQuestionBean, TestExamQuestionBean, String, TimeWindow>() {
                            @Override
                            public void process(String questionId,
                                                ProcessWindowFunction<TestExamQuestionBean, TestExamQuestionBean, String, TimeWindow>.Context ctx,
                                                Iterable<TestExamQuestionBean> iterable,
                                                Collector<TestExamQuestionBean> collector) throws Exception {
                                TestExamQuestionBean bean = iterable.iterator().next();
                                bean.setStt(AtguiguUtil.toDateTime(ctx.window().getStart()));
                                bean.setEdt(AtguiguUtil.toDateTime(ctx.window().getEnd()));
                                bean.setCurDate(AtguiguUtil.toDate(ctx.window().getStart()+1000));

                                collector.collect(bean);
                            }
                        });
    }


    private DataStream<TestExamQuestionBean> transFunc(DataStreamSource<String> stream) {

        SingleOutputStreamOperator<TestExamQuestionBean> answerStream = stream
                .map(json -> {
                    JSONObject obj = JSON.parseObject(json);
                    String questionId = obj.getString("question_id");
                    String isCorrect = obj.getString("is_correct");
                    Long ts = obj.getLong("ts") * 1000;
                    Long answerCorrectCt = 0l;

                    if ("1".equals(isCorrect)) {
                        answerCorrectCt = 1l;
                    }
                    return new TestExamQuestionBean(
                            "", "", "",
                            questionId, "",
                            answerCorrectCt, 1l,
                            0l, 0l,
                            ts
                    );

                });

        SingleOutputStreamOperator<TestExamQuestionBean> uAswerStream = stream
                .map(JSON::parseObject)
                .keyBy(json -> json.getString("user_id"))
                .process(new KeyedProcessFunction<String, JSONObject, TestExamQuestionBean>() {

                    private ValueState<String> lastState;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        lastState = getRuntimeContext().getState(new ValueStateDescriptor<String>("lastState", String.class));
                    }

                    @Override
                    public void processElement(JSONObject obj,
                                               KeyedProcessFunction<String, JSONObject, TestExamQuestionBean>.Context ctx,
                                               Collector<TestExamQuestionBean> collector) throws Exception {
                        String lastDay = lastState.value();
                        String questionId = obj.getString("question_id");
                        Long ts = obj.getLong("ts") * 1000;
                        String today = AtguiguUtil.toDate(ts);
                        if (!today.equals(lastDay)) {
                            lastState.update(today);
                            collector.collect(new TestExamQuestionBean(
                                    "", "", "",
                                    questionId, "",
                                    0l, 0l, 0l, 1l,
                                    ts
                            ));
                        }
                    }
                });

        SingleOutputStreamOperator<TestExamQuestionBean> uAnswerCorrectStream = stream
                .map(JSON::parseObject)
                .filter(obj -> "1".equals(obj.getString("is_correct")))
                .keyBy(obj -> obj.getString("user_id"))
                .process(new KeyedProcessFunction<String, JSONObject, TestExamQuestionBean>() {
                    private ValueState<String> lastState2;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        lastState2 = getRuntimeContext().getState(new ValueStateDescriptor<String>("lastState2", String.class));
                    }

                    @Override
                    public void processElement(JSONObject obj,
                                               KeyedProcessFunction<String, JSONObject, TestExamQuestionBean>.Context ctx,
                                               Collector<TestExamQuestionBean> collector) throws Exception {
                        String lastDay = lastState2.value();
                        String questionId = obj.getString("question_id");
                        Long ts = obj.getLong("ts") * 1000;
                        String today = AtguiguUtil.toDate(ts);
                        if (!today.equals(lastDay)) {
                            lastState2.update(today);
                            collector.collect(new TestExamQuestionBean(
                                    "", "", "",
                                    questionId, "",
                                    0l, 0l, 1l, 0l,
                                    ts
                            ));
                        }
                    }
                });

        return answerStream.union(uAswerStream, uAnswerCorrectStream);

    }
}
